var arrLayerList = new Array();

var sol = {};
sol.win = {};

sol.win.InitIndex = function(elementId) {
	for (var i = 0 ; i < arrLayerList.length ; i++) {
		try	{
			if (arrLayerList[i] != elementId) {
				arrLayerList[i].style.zIndex = arrLayerList[i].style.zIndex - 1;
			} else {
				arrLayerList[i].style.zIndex = 100;
			}			
		} catch (e)	{}
	}
}

sol.win.Layer = function(elementId) {
	this.element = document.getElementById(elementId);
	this.elementname = elementId;
	this.dragging = false; 
	this.resizing = false; 
	this.selected = false; 
	this.diff = null;
	this.curset = 5; 
	this.minlock = {
		x: 30, y: 30 
	};
	this.dir = ""
	this.size = null;
	
	this.mouseDown = ajax.Event.bindAsListener(this.doMouseDown, this);
	this.mouseMove = ajax.Event.bindAsListener(this.doMouseMove, this);
	this.mouseUp = ajax.Event.bindAsListener(this.doMouseUp, this);
	
	ajax.Event.addListener(this.element, "mousedown", this.mouseDown);
	ajax.Event.addListener(this.element, "mousemove", this.mouseMove);

	this.add(this.element); 
}

sol.win.Layer.prototype = {
	add: function(TargetLayer) {
		arrLayerList[arrLayerList.length] = TargetLayer; 
		TargetLayer.style.zIndex = 100; 
	},
	remove: function() { 
		var newList = new Array();
		for (var i = 0 ; i < arrLayerList.length ; i++) {
			if (arrLayerList[i] != this.elementname) {
				newList[newList.length] = arrLayerList[i];
			}
		}
		arrLayerList = newList;
		ajax.Event.removeListener(this.element, "mousedown", this.mouseDown);
		ajax.Event.removeListener(this.element, "mousemove", this.mouseMove);
	},
	doMouseDown: function(e) {
		var event = window.event || e;
		if (!ajax.Event.isLeftButton(event)) return;
		
		this.selected = true;
		
		var elementXY = ajax.GUI.getXY(this.element);
		var mouseXY = ajax.Event.getMouseXY(event);
		this.diff = {
			x: mouseXY.x - elementXY.x,
			y: mouseXY.y - elementXY.y,
			mx: mouseXY.x, 
			my: mouseXY.y  
		};		
		sol.win.InitIndex(this.element);


		ajax.Event.addListener(document, "mousemove", this.mouseMove);
		ajax.Event.addListener(document, "mouseup", this.mouseUp);
		ajax.Event.stopEvent(event);
	},
	doMouseMove: function(e) {
		var event = window.event || e;
		var mouseXY = ajax.Event.getMouseXY(event);		
		var elementXY = ajax.GUI.getXY(this.element);
		
		var xPos = mouseXY.x - elementXY.x;
		var yPos = mouseXY.y - elementXY.y;
		this.size = {
			x: parseInt( ajax.GUI.getStyle(this.element, 'width'), 10 ),
			y: parseInt( ajax.GUI.getStyle(this.element, 'height'), 10 )
		};
		if (!this.selected) {
			this.dir = "";

			if (yPos<this.curset) this.dir += "n"; 
			else if (yPos > this.element.offsetHeight-this.curset) this.dir += "s"; 
			if (xPos<this.curset) this.dir += "w"; 
			else if (xPos > this.element.offsetWidth-this.curset) this.dir += "e"; 
			if (yPos>this.curset && xPos>this.curset && xPos < this.element.offsetWidth-this.curset && yPos < this.element.offsetHeight-this.curset) {
				this.dir += "m";
			}
			if (this.dir == "") {
				this.element.style.cursor = "default";
			} else {
				if (this.dir!= "m") {
					this.element.style.cursor = this.dir + "-resize";
				} else {
					this.element.style.cursor = this.dir + "ove";
				}
			}
			return false;
		}
		
		if (this.dir != "default")
		{
			if (this.dir.indexOf("m") != -1) { 
				this.dragging = true;
				ajax.GUI.setOpacity(this.element, 0.60);
				var newXY = { 
					x: mouseXY.x - this.diff.x,
					y: mouseXY.y - this.diff.y
				}
				ajax.GUI.setXY(this.element, newXY.x, newXY.y);
			}
			if (this.dir.indexOf("n") != -1) { 
				this.resizeing = true;
				var newXY = { 
					height: this.size.y + (this.diff.my - mouseXY.y),
					y: mouseXY.y - this.diff.y	
				}
				this.diff.my = mouseXY.y;
				
				if (newXY.height > this.minlock.y) {
					ajax.GUI.setOpacity(this.element, 0.60);
					ajax.GUI.setXY(this.element, null, newXY.y);
					ajax.GUI.setSize(this.element, null, newXY.height);
				} else {
					this.doMouseUp(e);
				}
			}
			if (this.dir.indexOf("s") != -1) { 
				this.resizeing = true;
				var newXY = { 
					height: this.size.y - (this.diff.my - mouseXY.y), 
					y: mouseXY.y - this.diff.y	
				}
				this.diff.my = mouseXY.y;
				if (newXY.height > this.minlock.y) {
					ajax.GUI.setOpacity(this.element, 0.60);
					ajax.GUI.setSize(this.element, null, newXY.height);
				} else {
					this.doMouseUp(e);
				}
			}
			if (this.dir.indexOf("e") != -1) { 
				this.resizeing = true;
				var newXY = { 
					width: this.size.x - (this.diff.mx - mouseXY.x), 
					x: mouseXY.x - this.diff.x	
				}
				this.diff.mx = mouseXY.x;			
				if (newXY.width > this.minlock.x) {
					ajax.GUI.setOpacity(this.element, 0.60);
					ajax.GUI.setSize(this.element, newXY.width, null);
				} else {
					this.doMouseUp(e);
				}
			}
			if (this.dir.indexOf("w") != -1) { 
				this.resizeing = true;
				var newXY = { 
					width: this.size.x + (this.diff.mx - mouseXY.x),
					x: mouseXY.x + this.diff.x	
				}
				this.diff.mx = mouseXY.x;
				if (newXY.width > this.minlock.x) {
					ajax.GUI.setOpacity(this.element, 0.60);
					ajax.GUI.setSize(this.element, newXY.width, null);
					ajax.GUI.setXY(this.element, newXY.x, null);
				} else {
					this.doMouseUp(e);
				}
			}
		}				
		ajax.Event.stopEvent(event);
	},
	doMouseUp: function(e) {
		if (!this.selected) return;
		
		this.selected = false;
		this.diff = null;

		var event = window.event || e;
		if (this.dragging || this.resizeing) {
			this.resizeing = false;
			this.dragging = false;
			ajax.GUI.setOpacity(this.element, 1.0);
		}
		ajax.Event.removeListener(
			document, "mousemove", this.mouseMove);
		ajax.Event.removeListener(
			document, "mouseup", this.mouseUp);
		ajax.Event.stopEvent(event);
	}
}
				this.doMouseUp(e);
				}
			}
			if (this.dir.indexOf("e") != -1) { //동쪽
				this.resizeing = true;
				var newXY = { 
					width: this.size.x - (this.diff.mx - mouseXY.x), 
					x: mouseXY.x - this.diff.x	
				}
				this.diff.mx = mouseXY.x;			
				if (newXY.width > this.minlock.x) {
					ajax.GUI.setOpacity(this.element, 0.60);
					ajax.GUI.setSize(this.element, newXY.width, null);
				} else {
					this.doMouseUp(e);
				}
			}
			if (this.dir.indexOf("w") != -1) { //서쪽
				this.resizeing = true;
				var newXY = { 
					width: this.size.x + (this.diff.mx - mouseXY.x),
					x: mouseXY.x + this.diff.x	
				}
				this.diff.mx = mouseXY.x;
				if (newXY.width > this.minlock.x) {
					ajax.GUI.setOpacity(this.element, 0.60);
					ajax.GUI.setSize(this.element, newXY.width, null);
					ajax.GUI.setXY(this.element, newXY.x, null);
				} else {
					this.doMouseUp(e);
				}
			}
		}				
		ajax.Event.stopEvent(event);
	},
	doMouseUp: function(e) {
		if (!this.selected) return;
		
		this.selected = false;
		this.diff = null;

		var event = window.event || e;
		if (this.dragging || this.resizeing) {
			this.resizeing = false;
			this.dragging = false;
			ajax.GUI.setOpacity(this.element, 1.0);
		}
		ajax.Event.removeListener(
			document, "mousemove", this.mouseMove);
		ajax.Event.removeListener(
			document, "mouseup", this.mouseUp);
		ajax.Event.stopEvent(event);
	}
}
// ------------------------------------------------------------- Sol Layer End -